#******************************************************************
#
# Copyright 2016 Samsung Electronics All Rights Reserved.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

##
# CoAP-HTTP Proxy build script
##
Import('env')
import os
local_env = env.Clone()

# Remove -Werror build flag when building the 'coap_http_proxy' library  it
# currently has a build warning that still needs to be solved. see IOT-2539
while '-Werror' in local_env['CFLAGS']: local_env['CFLAGS'].remove('-Werror')

if env.get('RELEASE'):
    local_env.AppendUnique(CCFLAGS=['-Os'])
else:
    local_env.AppendUnique(CCFLAGS=['-g'])

target_os = env.get('TARGET_OS')
src_dir = env.get('SRC_DIR')

######################################################################
# Build flags
######################################################################
local_env.AppendUnique(CPPPATH=[
    'include',
    os.path.join(src_dir, 'resource/csdk/include'),
    os.path.join(src_dir, 'resource/csdk/stack/include'),
    os.path.join(src_dir, 'resource/csdk/connectivity/common/inc/'),
    os.path.join(src_dir, 'resource/csdk/security/include'),
    os.path.join(src_dir, 'extlibs/cjson'),
])

with_upstream_libcoap = local_env.get('WITH_UPSTREAM_LIBCOAP')
if with_upstream_libcoap == '1':
    local_env.AppendUnique(CPPPATH=['#/extlibs/libcoap/libcoap/include'])
else:
    local_env.AppendUnique(
        CPPPATH=['#/resource/csdk/connectivity/lib/libcoap-4.1.1/include'])

local_env.PrependUnique(LIBS=[
    'cjson',
    'oc',
    'octbstack',
    'oc_logger',
    'connectivity_abstraction',
    'coap',
])
if target_os not in ['windows']:
    local_env.AppendUnique(CXXFLAGS=['-O2', '-g', '-Wall', '-Wextra'])

if target_os in ['linux', 'tizen']:
    local_env.AppendUnique(LIBS=['pthread', 'curl', 'm'])

if target_os == 'android':
    local_env.AppendUnique(CXXFLAGS=['-frtti', '-fexceptions'])
    local_env.AppendUnique(LIBS=['gnustl_static'])

    if not env.get('RELEASE'):
        local_env.AppendUnique(LIBS=['log'])
######################################################################
# Source files and Targets
######################################################################
proxy_src = [
    './src/CoapHttpHandler.c',
    './src/CoapHttpMap.c',
    './src/CoapHttpParser.c',
]

if target_os in ['tizen', 'linux']:
    proxysdk = local_env.SharedLibrary('coap_http_proxy', proxy_src)
else:
    proxysdk = local_env.StaticLibrary('coap_http_proxy', proxy_src)

local_env.InstallTarget(proxysdk, 'coap_http_proxy')
local_env.UserInstallTargetLib(proxysdk, 'coap_http_proxy')
local_env.UserInstallTargetHeader(
    'include/CoapHttpHandler.h', 'service/coap-http-proxy', 'CoapHttpHandler.h')

######################################################################
# Samples for the proxy
######################################################################
env.AppendTarget('coap_http_proxy_sample', default=False)
if "coap_http_proxy_sample" in COMMAND_LINE_TARGETS and target_os in ['linux', 'tizen']:
    SConscript('samples/SConscript')

######################################################################
# Unit tests for the proxy
######################################################################
if local_env.get('WITH_TEST') and target_os in ('linux'):
    SConscript('unittests/SConscript')
